package com.boyuan.huijiaoyu;

import im.fir.sdk.FirSdk;

import java.util.Map;

import net.tsz.afinal.FinalDb;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.easemob.EMCallBack;
import com.example.huanxin.more.DemoHXSDKHelper;
import com.example.huanxin.more.User2;
import com.magicsoft.app.db.DataBaseCreateHelper;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;




public class ColorHolderApplication extends Application {

	private static ColorHolderApplication instance;
	public FinalDb shareDb = null;
	public MainActivity mainActivity = null;
	public static ColorHolderApplication getInstance() {
		if (null == instance) {
			instance = new ColorHolderApplication();
		}
		return instance;

	}
	
	//huanxin
	public static Context applicationContext;
	public final String PREF_USERNAME = "username";
	/**
	 * 当前用户nickname,为了苹果推送不是userid而是昵称
	 */
	public static String currentUserNick = "";
	public static DemoHXSDKHelper hxSDKHelper = new DemoHXSDKHelper();

	

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		FirSdk.init(this);
		super.onCreate();

		//huixin
		applicationContext = this;
		//极光推送
		JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
		JPushInterface.init(this);
		
		hxSDKHelper.onInit(applicationContext);

//		SDKInitializer.initialize(this);

		// 全局实例
		instance = this;
		JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
		JPushInterface.init(this);
		// 获取测试设备信息
		getDeviceInfo(getApplicationContext());
		// 创建数据库
		DataBaseCreateHelper DBhelper = new DataBaseCreateHelper(this);
		// JPush初始化推送

		try {
			DBhelper.createDataBase();

		} catch (Exception e1) {
			e1.printStackTrace();
		}

		// 读取数据库
		try {

			// TODO:设置调试模式，正式发布需要关闭调试
			shareDb = FinalDb.create(getApplicationContext(),
					DataBaseCreateHelper.DATABASE_FILENAME, false);

		} catch (Exception e) {
		}
		initImageLoader(getApplicationContext());
	}

	/**
	 * 获取测试设备信息
	 * 
	 * @param context
	 * @return
	 */
	public static String getDeviceInfo(Context context) {
		try {
			org.json.JSONObject json = new org.json.JSONObject();
			android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);

			String device_id = tm.getDeviceId();

			android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);

			String mac = wifi.getConnectionInfo().getMacAddress();
			json.put("mac", mac);

			if (TextUtils.isEmpty(device_id)) {
				device_id = mac;
			}

			if (TextUtils.isEmpty(device_id)) {
				device_id = android.provider.Settings.Secure.getString(
						context.getContentResolver(),
						android.provider.Settings.Secure.ANDROID_ID);
			}

			json.put("device_id", device_id);

			return json.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {

		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onLowMemory() {

		super.onLowMemory();
	}




	/**
	 * 获取内存中好友user list
	 *huanxin
	 * @return
	 */
	public Map<String, User2> getContactList() {
		
		return hxSDKHelper.getContactList();
	}

	/**
	 * 设置好友user list到内存中
	 *
	 * @param contactList
	 */
	public void setContactList(Map<String, User2> contactList) {
		hxSDKHelper.setContactList(contactList);
	}

	/**
	 * 获取当前登陆用户名
	 *
	 * @return
	 */
	public String getUserName() {
		return hxSDKHelper.getHXId();
	}

	/**
	 * 获取密码
	 *
	 * @return
	 */
	public String getPassword() {
		return hxSDKHelper.getPassword();
	}

	/**
	 * 设置用户名
	 *
	 * @param user
	 */
	public void setUserName(String username) {
		hxSDKHelper.setHXId(username);
	}

	/**
	 * 设置密码 下面的实例代码 只是demo，实际的应用中需要加password 加密后存入 preference 环信sdk
	 * 内部的自动登录需要的密码，已经加密存储了
	 *
	 * @param pwd
	 */
	public void setPassword(String pwd) {
		hxSDKHelper.setPassword(pwd);
	}

	/**
	 * 退出登录,清空数据
	 */
	public void logout(final EMCallBack emCallBack) {
		// 先调用sdk logout，在清理app中自己的数据
		hxSDKHelper.logout(emCallBack);
	}

}
